package com.wx.mall.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.common.exception.BusinessException;
import com.wx.mall.common.exception.code.CarriageTemplateResponseCode;
import com.wx.mall.common.api.BatchProcessingVo;
import com.wx.mall.common.exceptions.ApiException;
import com.wx.mall.sys.entity.CarriageTemplate;
import com.wx.mall.sys.mapper.CarriageTemplateMapper;
import com.wx.mall.sys.service.ICarriageTemplateService;
import com.wx.mall.sys.vo.req.CarriageTemplatePageReqVO;
import com.wx.mall.sys.vo.req.CarriageTemplateReqVO;
import com.wx.mall.sys.vo.resp.CarriageTemplateRespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  运费模板服务实现类
 * </p>
 *
 * @author ljl
 * @since 2020-03-20
 */
@Service
public class CarriageTemplateServiceImpl extends ServiceImpl<CarriageTemplateMapper, CarriageTemplate> implements ICarriageTemplateService {

    @Override
    public Page<CarriageTemplate> pageInfo(CarriageTemplatePageReqVO vo) {
        // mybatis-plus 条件查询
        QueryWrapper<CarriageTemplate> wrapper = new QueryWrapper<>();
        // 所有未删数据
        wrapper.eq("state", "0");

        return page(vo,wrapper);
    }

    @Override
    public List<CarriageTemplateRespVO> availableInfos() {
        List<CarriageTemplateRespVO> list = new ArrayList<>();

        // mybatis-plus 条件查询
        QueryWrapper<CarriageTemplate> wrapper = new QueryWrapper<>();
        // 所有未删数据
        wrapper.eq("state", "0");
        List<CarriageTemplate> templates = baseMapper.selectList(wrapper);
//        if (templates.size() <=0 ){
//            throw new ApiException("查询运费模板失败！");
//        }

        for (CarriageTemplate template : templates){
            CarriageTemplateRespVO vo = new CarriageTemplateRespVO();
            //将CarriageTemplate实体中的数据复制到vo中
            BeanUtils.copyProperties(template, vo);
            list.add(vo);
        }
        return list;
    }

    @Override
    public Page<CarriageTemplate> pageList(CarriageTemplatePageReqVO vo) {
//        // mybatis-plus 条件查询
//        QueryWrapper<CarriageTemplate> wrapper = new QueryWrapper<>();

        return page(vo,new QueryWrapper<>());
    }

    /**
     * 分页查询
     * @param vo
     * @param wrapper
     * @return
     */
    private Page<CarriageTemplate> page(CarriageTemplatePageReqVO vo,QueryWrapper<CarriageTemplate> wrapper){
        //分页参数
        Page<CarriageTemplate> page = new Page<CarriageTemplate>(vo.getPageNum(), vo.getPageSize());
        //分页结果页面
        Page<CarriageTemplate> carriageTemplatePage = null;
        //排序方式
        int sort = vo.getSort();
        // 排序方式
        wrapperSort(wrapper,sort);

        carriageTemplatePage = baseMapper.selectPage(page, wrapper);

        return carriageTemplatePage;
    }

    /**
     * 排序方式
     * @param wrapper
     * @param sort
     */
    private void wrapperSort(QueryWrapper<CarriageTemplate> wrapper,Integer sort){
        if (sort == 0){// 0 编号
            wrapper.orderBy(true,true,"id");
        }else if (sort == 1){// 1 名称
            wrapper.orderByAsc("name");
        }else if (sort == 2){// 2 计费方式
            wrapper.orderByAsc("pricing_model");
        }else if (sort == 3){// 3 首重
            wrapper.orderByAsc("first_weight");
        }else if (sort == 4){// 4 首费
            wrapper.orderByAsc("first_expense");
        }else if (sort == 5){// 5 续重
            wrapper.orderByAsc("extend_weight");
        }else if (sort == 6){// 6 续费
            wrapper.orderByAsc("extend_expense");
        }else if (sort == 7){// 7 启用状态
            wrapper.orderByAsc("region");
        }else if (sort == 8){// 8 添加时间
            wrapper.orderByDesc("time");
        }else if (sort == 9){// 8 添加时间
//            wrapper.orderBy(true,false,"time");
            wrapper.orderByAsc("enabled");
        }
//        return wrapper;
    }

    @Override
    public void softDeleteById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        // 删除数据
        map.put("state","1");
        // 停用数据
        map.put("enabled", 1);
        // 要删除的id
        map.put("id",id);

        //通过id修改数据状态
        int result = baseMapper.updateInfoById(map);
        if (result == 0){
            throw new BusinessException(CarriageTemplateResponseCode.DELETE_ERROR);
        }
    }

    @Override
    public void batchOperation(BatchProcessingVo vo) {
        Map<String, Object> params = new HashMap<>();
        // 操作是否有效
        Integer isEnabled = 0;
        // 要执行什么操作 0 修改（还原） 1 删除 2 启用 3 停用
        if (vo.getType() == 0) {
            params.put("state", "0");
            isEnabled = 1;
        } else if (vo.getType() == 1){
            params.put("state", "1");
            params.put("enabled", 1);
            isEnabled = 1;
        } else if (vo.getType() == 2){
            params.put("enabled", 0);
            isEnabled = 1;
        } else if (vo.getType() == 3){
            params.put("enabled", 1);
            isEnabled = 1;
        }

        if (isEnabled == 1) {
            for (Integer id : vo.getIds()) {
                params.put("id", id);
                baseMapper.updateInfoById(params);
            }
        }
    }

    @Override
    public CarriageTemplateRespVO findById(Integer id) {
        //通过id获取数据
        CarriageTemplate carriageTemplate = baseMapper.selectOne(new QueryWrapper<CarriageTemplate>().eq("id", id));
        if (carriageTemplate == null) {//查询失败 抛出异常
            throw new BusinessException(CarriageTemplateResponseCode.QUERY_ERROR);
        }
        CarriageTemplateRespVO vo = new CarriageTemplateRespVO();
        //将CarriageTemplate实体中的数据复制到vo中
        BeanUtils.copyProperties(carriageTemplate, vo);
        return vo;
    }

    @Override
    public void updateById(CarriageTemplateReqVO vo) {
        CarriageTemplate carriageTemplate = new CarriageTemplate();
        //将vo中的数据复制到实体类中
        BeanUtils.copyProperties(vo, carriageTemplate);
        carriageTemplate.setState("0");
        //通过id更新数据
        int result = baseMapper.updateById(carriageTemplate);
        if (result == 0){
            throw new BusinessException(CarriageTemplateResponseCode.UPDATE_ERROR);
        }
    }

    @Override
    public void add(CarriageTemplateReqVO vo) {
        CarriageTemplate carriageTemplate = new CarriageTemplate();
        //将vo中的数据复制到实体类中
        BeanUtils.copyProperties(vo, carriageTemplate);
        int result = baseMapper.insert(carriageTemplate);
        if (result == 0){
            throw new BusinessException(CarriageTemplateResponseCode.INSERT_ERROR);
        }
    }

    @Override
    public void updateEnabledById(Integer id, Integer enabled) {
        //封装条件
        Map<String, Object> params = new HashMap<>();
        params.put("id",id);
        params.put("enabled",enabled);
        //执行更新操作
        int result = baseMapper.updateInfoById(params);
        if (result == 0){
            throw new BusinessException(CarriageTemplateResponseCode.UPDATE_ERROR);
        }
    }
}
